package com.qk.management.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.param.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.entity.User;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public PageResult<BusinessDO> selectByPage(BusinessListDTO businessListDTO) {
        PageHelper.startPage(businessListDTO.getPage(), businessListDTO.getPageSize());
        List<BusinessDO> list = businessMapper.selectByPage(businessListDTO);
        Page<BusinessDO> page = (Page<BusinessDO>) list;
        return PageResult.<BusinessDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void add(BusinessDTO businessDTO) {
        if(ObjectUtil.isEmpty(businessDTO.getName())||ObjectUtil.isEmpty(businessDTO.getPhone())){
            CommonException.throwException(ParamEnum.PARAM_ERROR);
        }
        Business business = BeanUtil.copyProperties(businessDTO, Business.class);
        business.setStatus(BusinessStatusConstants.STATUS_WAIT);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        this.save(business);
    }

    @Override
    public BusinessVO selectById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        Integer userId = business.getUserId();
        User user = userMapper.selectUserById(userId);
        List<BusinessTrackRecord> businessTrackRecordList = businessTrackRecordMapper.selectList(Wrappers.lambdaQuery(BusinessTrackRecord.class).eq(BusinessTrackRecord::getBusinessId, id));
        BusinessVO businessVO = BeanUtil.copyProperties(business, BusinessVO.class);
        List<BusinessVO.BusinessTrackRecordVO> list = businessTrackRecordList.stream().map(businessTrackRecord -> {
            BusinessVO.BusinessTrackRecordVO businessTrackRecordVO = BeanUtil.copyProperties(businessTrackRecord, BusinessVO.BusinessTrackRecordVO.class);
            businessTrackRecordVO.setAssignName(user.getName());
            return businessTrackRecordVO;
        }).toList();
        businessVO.setTrackRecords(list);
        return businessVO;
    }

    @Override
    @Transactional
    public void updateBusinessInfoById(UpdateBusinessInfoDTO dto) {
        Business business = BeanUtil.copyProperties(dto, Business.class);
        business.setUpdateTime(LocalDateTime.now());
        if(ObjectUtil.equal(dto.getStatus(), BusinessStatusConstants.STATUS_WAIT_FOLLOW)){
            business.setStatus(BusinessStatusConstants.STATUS_FOLLOW);
        }
        this.baseMapper.updateById(business);
        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setId(null);
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserContextHolders.get());
        businessTrackRecord.setKeyItems(dto.getKeyItems().toString());
        businessTrackRecord.setTrackStatus(dto.getTrackStatus());
        this.businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public void toCustomer(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.STATUS_TO_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
        Customer customer = BeanUtil.copyProperties(business, Customer.class);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> selectPool(BusinessPoolDTO businessPoolDTO) {
        PageHelper.startPage(businessPoolDTO.getPage(), businessPoolDTO.getPageSize());
        List<Business> list = businessMapper.selectPool(businessPoolDTO);
        Page<Business> page = (Page<Business>) list;
        return PageResult.<Business>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }
}
